package com.qire.manhua.controller;

import android.content.Intent;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.R;
import com.qire.manhua.activity.HomeActivity;
import com.qire.manhua.databinding.FragmentBookshelfDownloadBinding;
import com.qire.manhua.databinding.LayoutHomeBookshelfDownloadItemBinding;
import com.qire.manhua.fragment.BookshelfDownloadFragment;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.BookshelfDownloadViewModel;
import com.qire.manhua.model.viewModel.impl.BookshelfDownloadViewModel.DownloadItemViewModel;

import androidx.annotation.NonNull;

public class BookshelfDownloadController extends EventController<BookshelfDownloadFragment> implements AutoVariable<LayoutHomeBookshelfDownloadItemBinding> {

    private BookshelfDownloadViewModel viewModel;

    public BookshelfDownloadController(@NonNull BookshelfDownloadFragment owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        FragmentBookshelfDownloadBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setBookshelfDownloadController(this);
        }
    }

    @Override
    public void bindVariable(LayoutHomeBookshelfDownloadItemBinding viewBind) {
        viewBind.setBookshelfDownloadController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BookshelfDownloadViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**********************************************************************************************/

    public void exceptionBtnOnClick(View view) {
        HomeActivity homeActivity = ObjectUtil.as(owner.getActivity(), HomeActivity.class);
        if(ObjectUtil.nonNull(homeActivity)) {
            homeActivity.gotoTabIndex(PageTabIndex.TAB_INDEX_HOME_RECOMMEND, PageTabIndex.TAB_INDEX_INVALID);
        }
    }

    public void delBtnOnClick(View view) {
        int batchDeleteCount = viewModel.batchDeleteCount();

        if(batchDeleteCount < 1) {
            return;
        }

        DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_l)
                .setTextView(R.id.dialog_title, "共选中" + batchDeleteCount + "个作品")
                .setTextView(R.id.dialog_content, "是否删除已选中作品?")
                .setTextView(R.id.dialog_button_light, "取消", cancelView -> DialogFastBuildUtil.dismissDialogBy(cancelView))
                .setTextView(R.id.dialog_button_dark, "确定", confirmView -> {
                    viewModel.batchDelDownloadRecord();
                    DialogFastBuildUtil.dismissDialogBy(confirmView);
                })
                .show();
    }

    public void itemViewOnClick(View view){
        DownloadItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DownloadItemViewModel.class);

        if(ObjectUtil.isNull(itemViewModel)) {
            return;
        }

        if(itemViewModel.isEditMode()) {
            checkBoxBtnOnClick(view);
            return;
        }

        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDownloadProgressPage)
                .withData("comicBookEntity", itemViewModel.toComicBookEntity())
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .navigation();
    }

    public void checkBoxBtnOnClick(View view) {
        DownloadItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DownloadItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.toggleWhetherChoose();
//            fragment.noticeSelectListener();
        }
    }

    public void operationBtnOnClick(View view) {

        DownloadItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DownloadItemViewModel.class);

        if(ObjectUtil.nonNull(itemViewModel)) {
            if(itemViewModel.isComplete()) {
                AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                        .withData("comicBookEntity", itemViewModel.toComicBookEntity())
                        .navigation();
                return;
            }

            itemViewModel.toggleDownloadStatus();
        }

    }

}
